// pages/my/my.js
const { getUser, getUser_admin } = require('../../http/api')
const { SET } = require('../../util/login/API')
const {add} = require('../../util/getCart/getCart')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    code: 1,
    data: {},
    admin: {},
    res: {}
  },
  // 事件
  login() {
    wx.getUserProfile({
      lang: "zh_CN",
      desc: "获取微信信息",
      success: (res) => {
        let token = wx.getStorageSync('token')
        let { country, nickName, city, avatarUrl, gender } = res.userInfo
        SET(avatarUrl, city, nickName, token, country, gender).then(res => {
          this.onall()
        })
      }
    })
  },
  grow(){
    wx.navigateTo({
      url: '/pages/grow/grow',
    })
  },
  help:function(){
    wx.navigateTo({
      url: '/pages/help/help',
    })
  },
  // 系统设置路由
  xitong(){
    wx.navigateTo({
      url: '/pages/xitong/xitong',
    })
  },
  // 订单
  DD(e) {
    // console.log(e.currentTarget.dataset.val);
    wx.navigateTo({
      url: `/pages/my_tab/my_tab?val=${e.currentTarget.dataset.val}`,
    })
  },
  // 三级分销
  distributor() {
    wx.navigateTo({
      url: `/pages/distributor/distributor`,
    })
  },
  // 积分券兑换积分
  calculus() {
    wx.navigateTo({
      url: `/pages/calculus/calculus`,
    })
  },
  grxx() {
    wx.navigateTo({
      url: `/pages/grxx/grxx`,
    })
  },
  huishoudingdan(){
    wx.navigateTo({
      url: `/pages/huishoudingdan/huishoudingdan`,
    })
  },
  gps(){
    wx.navigateTo({
      url: `/pages/GPS/gps`,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onall() {
    // 获取用户信息
    getUser_admin(wx.getStorageSync('token')).then(res => {
      console.log(res);
      // let {nick,avatarUrl} = res.data.base
      if (res.data.code == 0) {
        if (res.data.data.base.nick !== undefined) {
          this.setData({
            code: 0
          })
          add()
        } else {
          this.setData({
            code: 1
          })
        }
      }
      this.setData({
        admin: res.data.data.base
      })
    })
    // 获取用户资产
    getUser(wx.getStorageSync('token')).then(res => {
      this.setData({
        data: res.data.data
      })
    })
    add()
  },
  onShow: function () {
    this.onall()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})